<template>
  <div class="com-container">
    <div class="com-chart"
         ref="hotRef">
    </div>
    <!-- 左右箭头 -->
    <span class="iconfont icon-arrow-lift arr-left"
          @click="toLeft"
          :style="fontSizeStyle"></span>
    <span class="iconfont icon-arrow-right arr-right"
          @click="toRight"
          :style="fontSizeStyle"></span>
    <span class="title"
          :style="fontSizeStyle">{{title}}</span>
  </div>
</template>

<script>
import { getHotData } from 'network/hot.js';
export default {
  name: 'Hot',
  data () {
    return {
      echartsInstance: null,
      allData: null, //服务器返回的数据
      currentIndex: 0, //当前一级目录的索引值
      titleFontSize: 0, //标题的字体大小
    }
  },
  mounted () {
    this.initChart();
    this.getData();
    this.adaptScreen();
    window.addEventListener('resize', this.adaptScreen);
  },
  destroyed () {
    window.removeEventListener('resize', this.adaptScreen);
  },
  computed: {
    //一级标题名字
    title () {
      if (!this.allData) {
        return [];
      }
      return this.allData[this.currentIndex].name;
    },
    //大小样式
    fontSizeStyle () {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    }
  },
  methods: {
    //初始化echarts实例化对象
    initChart () {
      this.echartsInstance = this.$echarts.init(this.$refs.hotRef, 'chalk');
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: '▎热销商品销售金额占比统计',
          left: 20,
          top: 20,
        },
        //图例
        legend: {
          top: '15%',
          icon: 'circle'
        },
        //提示框组件
        tooltip: {
          show: true,
          formatter: arg => {
            // console.log(arg);
            return arg.data.thirdCategoryInfo;
          }
        },
        series: [{
          type: 'pie',
          //饼图上的文本标签
          label: {
            show: false
          },
          //高亮状态下显示
          emphasis: {
            label: {
              show: true
            }
          }
        }]
      };
      this.echartsInstance.setOption(initOption);
    },

    //获取服务器的数据
    async getData () {
      const { data: res } = await getHotData();
      this.allData = res;
      console.log(this.allData);
      this.updateChart();
    },

    //更新图表
    updateChart () {
      const legendData = this.allData[this.currentIndex].children.map(item => {
        return item.name;
      })
      const seriesData = this.allData[this.currentIndex].children.map(item => {
        //计算出所有三级分类的数值总和
        let total = 0;
        item.children.forEach(item => {
          total += item.value;

        });
        //三级分类要展示的信息
        let thirdCategoryInfo = '';
        item.children.forEach(item => {
          thirdCategoryInfo += `${item.name}: ${(item.value / total * 100).toFixed(2)}%<br/>`;
        });
        //console.log(thirdCategoryInfo);
        return {
          name: item.name,
          value: item.value,
          thirdCategoryInfo: thirdCategoryInfo
        }
      })

      const dataOption = {
        legend: {
          data: legendData
        },
        series: [
          {
            data: seriesData
          }
        ]
      };
      this.echartsInstance.setOption(dataOption);

    },

    //浏览器窗口发生变化，调用
    adaptScreen () {
      this.titleFontSize = this.$refs.hotRef.offsetWidth / 100 * 3.6;
      //和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        legend: {
          itemWidth: this.titleFontSize / 1.5,
          itemHeight: this.titleFontSize / 1.5,
          itemGap: this.titleFontSize / 1.5,
          textStyle: {
            fontSize: this.titleFontSize / 1.5
          }
        },
        series: [
          {
            radius: this.titleFontSize * 4.5, //饼图的半径
            center: ['50%', '60%'], //饼图的中心（圆心）坐标
          }
        ]
      };
      this.echartsInstance.setOption(adapterOption);
      this.echartsInstance.resize();
    },

    //点击了左箭头
    toLeft () {
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1;
      }
      this.updateChart();
    },

    //点击了右箭头
    toRight () {
      this.currentIndex++;
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0;
      }
      this.updateChart();
    }


  },
}
</script>

<style lang="less" scoped>
@import "~assets/font/iconfont.css";
.arr-left,
.arr-right {
  position: absolute;
  top: 60%;
  transform: translateY(-60%);
  color: #fff;
  cursor: pointer;
}

.arr-left {
  left: 20%;
}

.arr-right {
  right: 20%;
}

.title {
  position: absolute;
  right: 10%;
  bottom: 8%;
  color: #fff;
}
</style>